package git.yyge.test.api.stu;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import git.yyge.test.api.stu.req.StudentReq;
import git.yyge.test.entity.Student;
import git.yyge.test.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生控制器
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 根据ID查询用户
     * GET /api/users/{id}
     */
//    @GetMapping("/test//{id}")
//    public String getUserById(@PathVariable Long id) {
//        Student student = new Student();
//        student.setId(1); //学号
//        student.setName("李四"); //学号
//        student.setSex(0); //性别
//        student.setAge(18); //年龄
//        student.setClassNum(5); //班级
//
//
//        int insert = studentMapper.insert(student);
//        if(insert>0){
//            System.out.println("插入数据库成功！！");
//        }else {
//            System.out.println("插入失败");
//        }
//        return "xxxxxxxxxx你访问的get请求！传入id为"+id;
//    }
    @PostMapping("/add/stu")
    @ResponseBody
    public String getPostTest(@RequestBody StudentReq req) {
        Assert.notNull(req.getName(), "学生名称不能为空！");
        Assert.notNull(req.getAge(), "年龄不能为空！");
        //todo...

        Assert.notNull(req.getClassNum(), "班级不能为空！");
        Assert.notNull(req.getSex(), "性别不能为空！");


        Student student = new Student();
        student.setName(req.getName());
        student.setAge(req.getAge());
        student.setSex(req.getSex());
        student.setClassNum(req.getClassNum());


        //校验逻辑
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getName, req.getName());
        wrapper.eq(Student::getClassNum, req.getClassNum());
        wrapper.eq(Student::getSex, req.getSex());
//        QueryWrapper<> queryWrapper = new QueryWrapper(student);
        Long aLong = studentMapper.selectCount(wrapper);//
        Student selectStu = studentMapper.selectOne(wrapper);//
       // studentMapper.selectList();
        System.out.println(selectStu);

        if (selectStu != null) {
            return "不要重复录入!!";
        }

        int insert = studentMapper.insert(student);
        if (insert > 0) {
            System.out.println("插入数据库成功！！");
        } else {
            System.out.println("插入失败");
        }
        return "录入成功！！";
    }


    @GetMapping("/list/{classNum}")
    public List<Student> getUserById(@PathVariable Integer classNum) {

        //校验逻辑
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getClassNum, classNum);

//        QueryWrapper<> queryWrapper = new QueryWrapper(student);
        List<Student> list = studentMapper.selectList(wrapper);//

//        System.out.println(selectStu);

        return list;
    }
}
